package com.het.monitor.util;

import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtils {

    public final static String CHINA_LOCALE = "zh";
    public final static String CHINA_TIME_ZONE = "GMT+8";
    public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_mm_dd = "yyyyMMdd";
    public static final String YYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String yyyy = "yyyy";
    public static final String yyyy_en = "yyyy年";
    public static final String yyyy_mm = "yyyyMM";
    public static final String yyyy_mm_en = "yyyy年MM月";
    public static final String mm_dd_en = "MM月dd日";
    public static final String YYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_EN = "yyyy年MM月dd日";
    public static final String YYY_MM = "yyyy-MM";

    public static String getYearMonthLastDay(String year, String month){
        YearMonth yearMonth = YearMonth.of(Integer.valueOf(year),Integer.valueOf(month));
        int lastDay = yearMonth.lengthOfMonth();
        return year+"-"+ month +"-" + lastDay;
    }

    public static String getYear() {
        SimpleDateFormat format = new SimpleDateFormat(yyyy);
        String year = format.format(new Date());
        return year;
    }




    /**
     * 获取时间
     *
     * @param date    日期
     * @param pattern 格式
     * @return
     */
    public static String getTime(Date date, String pattern) {
        Date tempDate = date;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(dateToDateTime(tempDate));
    }



    /**
     * 获取第二天时间
     * @param date
     * @param pattern
     * @return
     */
    public static String getNextDayTime(Date date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,1);
        Date nextDate = calendar.getTime();
        return formatter.format(dateToDateTime(nextDate));
    }

    /**
     * date转dateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateToDateTime(Date date) {
        Instant instant = date.toInstant();
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * 获取指定格式的当地时间
     * @return
     */
    public static String getNowTime(String partten){
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = localDateTime.format(DateTimeFormatter.ofPattern(partten));
        return format;
    }

    /**
     * 或取当前时间之后的几个小时之后的时间
     * @param date 录入指定的日期
     * @param hour 指定日期之后的几个小时
     * @return
     */
    public static Date getAfterHourDate(Date date,int hour){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, hour);
        return ca.getTime();
    }

    /**
     * 获取当前航季
     * @return
     */
    public static String getSeasonTime(){
        try {
            SimpleDateFormat dfDate = new SimpleDateFormat(YYY_MM_DD);
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            String dseason_key = (year - 1) + "02";
            String xseason_key = year + "01";
            String ddseason_key = year + "02";

            //当前3月份最后一个星期天
            Date aprilLastSunday = getLastSunday(Calendar.APRIL, year);
            //明年3月份
            Date aprilLastSunday_nextYear= getLastSunday(Calendar.APRIL, year+1);
            //当前10月份最后一个星期天
            Date hourLastSunday = getLastSunday(Calendar.HOUR, year);
            //去年10月份
            Date hourLastSunday_lastYear = getLastSunday(Calendar.HOUR, year-1);

            //去年10月 到 今年3月份
            Date dbdt = dfDate.parse(getTime(hourLastSunday_lastYear,YYY_MM_DD));
            cal.setTime(aprilLastSunday);
            cal.add(Calendar.DATE,-1);
            Date dedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));

            //今年3月份 到 10月份
            Date xbdt = dfDate.parse(getTime(aprilLastSunday,YYY_MM_DD));
            cal.setTime(hourLastSunday);
            cal.add(Calendar.DATE,-1);
            Date xedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));

            //今年10月份 到 明年3月份
            Date ddbdt = dfDate.parse(getTime(hourLastSunday,YYY_MM_DD));
            cal.setTime(aprilLastSunday_nextYear);
            cal.add(Calendar.DATE,-1);
            Date ddedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));

            cal.setTime(dfDate.parse(getTime(new Date(),YYY_MM_DD)));
            if (cal.getTime().equals(dbdt) || cal.getTime().equals(dedt)) {
                return dseason_key;
            } else if (cal.getTime().before(dedt) && cal.getTime().after(dbdt)) {
                return dseason_key;
            }

            if (cal.getTime().equals(xbdt) || cal.getTime().equals(xedt)) {
                return xseason_key;
            } else if (cal.getTime().before(xedt) && cal.getTime().after(xbdt)) {
                return xseason_key;
            }

            if (cal.getTime().equals(ddbdt) || cal.getTime().equals(ddedt)) {
                return ddseason_key;
            } else if (cal.getTime().before(ddedt) && cal.getTime().after(ddbdt)) {
                return ddseason_key;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定年份月的最后一个星期天
     * @param month
     * @param year
     * @return
     */
    public static Date getLastSunday(int month, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1);
        int dayOftheWeek = cal.get(Calendar.DAY_OF_WEEK);
        int val = dayOftheWeek == Calendar.SUNDAY ? -7: -(dayOftheWeek-1);
        cal.add(Calendar.DAY_OF_MONTH, val);
        return cal.getTime();
    }

    /**
     * 获取当前航季
     * @return
     */
    public static String getSeasonStartEndTime(){
        try {
            SimpleDateFormat dfDate = new SimpleDateFormat(YYY_MM_DD);
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);

            //当前3月份最后一个星期天
            Date aprilLastSunday = getLastSunday(Calendar.APRIL, year);
            //明年3月份
            Date aprilLastSunday_nextYear= getLastSunday(Calendar.APRIL, year+1);
            //当前10月份最后一个星期天
            Date hourLastSunday = getLastSunday(Calendar.HOUR, year);
            //去年10月份
            Date hourLastSunday_lastYear = getLastSunday(Calendar.HOUR, year-1);

            //去年10月 到 今年3月份
            Date dbdt = dfDate.parse(getTime(hourLastSunday_lastYear,YYY_MM_DD));
            cal.setTime(aprilLastSunday);
            cal.add(Calendar.DATE,-1);
            Date dedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));

            System.out.println(getTime(dbdt,yyyy_mm_dd) + "_" + getTime(dedt,yyyy_mm_dd));

            //今年3月份 到 10月份
            Date xbdt = dfDate.parse(getTime(aprilLastSunday,YYY_MM_DD));
            cal.setTime(hourLastSunday);
            cal.add(Calendar.DATE,-1);
            Date xedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));

            System.out.println(getTime(xbdt,yyyy_mm_dd) + "_" + getTime(xedt,yyyy_mm_dd));
            //今年10月份 到 明年3月份
            Date ddbdt = dfDate.parse(getTime(hourLastSunday,YYY_MM_DD));
            cal.setTime(aprilLastSunday_nextYear);
            cal.add(Calendar.DATE,-1);
            Date ddedt = dfDate.parse(getTime(cal.getTime(),YYY_MM_DD));
            System.out.println(getTime(ddbdt,yyyy_mm_dd) + "_" + getTime(ddedt,yyyy_mm_dd));

            cal.setTime(dfDate.parse(getTime(new Date(),YYY_MM_DD)));
            if (cal.getTime().equals(dbdt) || cal.getTime().equals(dedt)) {
                return getTime(dbdt,yyyy_mm_dd) + "_" + getTime(dedt,yyyy_mm_dd);
            } else if (cal.getTime().before(dedt) && cal.getTime().after(dbdt)) {
                return getTime(dbdt,yyyy_mm_dd) + "_" + getTime(dedt,yyyy_mm_dd);
            }

            if (cal.getTime().equals(xbdt) || cal.getTime().equals(xedt)) {
                return getTime(xbdt,yyyy_mm_dd) + "_" + getTime(xedt,yyyy_mm_dd);
            } else if (cal.getTime().before(xedt) && cal.getTime().after(xbdt)) {
                return  getTime(xbdt,yyyy_mm_dd) + "_" + getTime(xedt,yyyy_mm_dd);
            }

            if (cal.getTime().equals(ddbdt) || cal.getTime().equals(ddedt)) {
                return  getTime(ddbdt,yyyy_mm_dd) + "_" + getTime(ddedt,yyyy_mm_dd);
            } else if (cal.getTime().before(ddedt) && cal.getTime().after(ddbdt)) {
                return getTime(ddbdt,yyyy_mm_dd) + "_" + getTime(ddedt,yyyy_mm_dd);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间格式转换
     * @param date 日期
     * @param thisFormat 原有格式
     * @param toFormat 转换后格式
     * @return
     */
    public static String getTimeToFormat(String date, String thisFormat, String toFormat) {
        SimpleDateFormat sm = new SimpleDateFormat(toFormat);
        SimpleDateFormat sms = new SimpleDateFormat(thisFormat,  Locale.UK);
        try {
            Date date_new = sms.parse(date);
            return sm.format(date_new);
        } catch (ParseException e) {
//            e.printStackTrace();
        }
        return null;
    }
    public static String getToDay() {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        return f.format(today);
    }

    public static String getYesterDay() {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date nowDay = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(nowDay);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date tomorrow = c.getTime();
        return f.format(tomorrow);
    }

    /**
     * 截取年月日
     *
     * @param startTime
     * @return
     */
    public static String subYMD(Object startTime) {
        if(startTime == null){
            return null;
        }
        return startTime.toString().substring(0,10);
    }
}
